package com.kgame5.xtank3d.app.gamecore.main {
import kgame5.enginex.GamePlayX;
import kgame5.enginex.entityimpl.LogicEntity;
import kgame5.kutil.KEvent;
import kgame5.kutil.T;

import com.kgame5.xtank3d.C;
import com.kgame5.xtank3d.GameEnum;
import com.kgame5.xtank3d.control.AddNPCTankCommand;
import com.kgame5.xtank3d.control.AddShotCommand;
import com.kgame5.xtank3d.control.AddTankCommand;
import com.kgame5.xtank3d.control.BlastChainCommand;
import com.kgame5.xtank3d.control.CartBlastCommand;
import com.kgame5.xtank3d.control.RemoveEntityCommand;
import com.kgame5.xtank3d.control.ShotBlastCommand;
import com.kgame5.xtank3d.control.TankReqFireShotCommand;
import com.kgame5.xtank3d.model.block.BlockEntity;
import com.kgame5.xtank3d.model.camera.MyCameraEntity;
import com.kgame5.xtank3d.model.ground.GroundEntity;
import com.kgame5.xtank3d.resource.ResourceManagerX;
import com.kgame5.xtank3d.script.LevelManager;
import com.kgame5.xtank3d.vo.BlockVO;
import com.kgame5.xtank3d.vo.GroundVO;
import com.kgame5.xtank3d.vo.MapVO;
import com.kgame5.xtank3d.vo.MyTankVO;
import com.kgame5.xtank3d.vo.level.CorpsVO;
import com.kgame5.xtank3d.vo.level.LevelVO;

import flash.display.Sprite;
import flash.events.EventDispatcher;

/**
 * @author kk
 */
public class GameEngineComp extends EventDispatcher{
	
	//通知mediator用
	public static const 
		EVENTTYPE_NEW_CORPS:String="EVENTTYPE_NEW_CORPS",//新兵团出现
		//EVENTTYPE_ADD_NPCTANK:String="EVENTTYPE_ADD_NPCTANK",//出现新坦克
		EVENTTYPE_MISSION_COMPLETE:String="EVENTTYPE_MISSION_COMPLETE",//全灭敌人
		EVENTTYPE_GAMEOVER:String="EVENTTYPE_GAMEOVER";//游戏结束
	
	private var gamePlay:GamePlayX;//游戏核心
	private var levelManager:LevelManager;//关卡管理器
	
	private var mc_c:Sprite;//容器
	public function GameEngineComp(mc_c:Sprite){
		this.mc_c=mc_c;
	}
	
	public function start():void{
		//this.mouseEnabled=false;
		//this.mouseChildren=false;
		
		gamePlay=new GamePlayX(C.STAGE_WIDTH, C.STAGE_HEIGHT, C.FRAME_RATE, false);
		
		mc_c.addChild(gamePlay.getDO());
		
		levelManager=new LevelManager(ResourceManagerX.getInstance().getLevelVO());
		
		gamePlay.addEventListener(BlastChainCommand.EVENTTYPE_TANK_DIE, onGamePlay_tankDie);
		
		levelManager.addEventListener(LevelManager.EVENTTYPE_ADD_NPCTANK, onLevelManager_addNPCTank);
		levelManager.addEventListener(LevelManager.EVENTTYPE_NEW_CORPS, onLevelManager_newCorps);
		levelManager.addEventListener(LevelManager.EVENTTYPE_MISSION_COMPLETE, onLevelManager_missionComplete);	
	
		//注册command
		gamePlay.registerCommand(TankReqFireShotCommand.NAME, new TankReqFireShotCommand());
		gamePlay.registerCommand(ShotBlastCommand.NAME, new ShotBlastCommand());
		gamePlay.registerCommand(CartBlastCommand.NAME, new CartBlastCommand());
		
		gamePlay.registerCommand(AddTankCommand.NAME, new AddTankCommand());
		gamePlay.registerCommand(AddNPCTankCommand.NAME, new AddNPCTankCommand());
		gamePlay.registerCommand(AddShotCommand.NAME, new AddShotCommand());
		gamePlay.registerCommand(RemoveEntityCommand.NAME, new RemoveEntityCommand());
		
		gamePlay.registerCommand(BlastChainCommand.NAME, new BlastChainCommand());
		gamePlay.registerCommand(CartBlastCommand.NAME, new CartBlastCommand());
		
		//准备好场景,背景，摄像机啥的
		this.prepare();
	}
	
	public function clear():void{
		gamePlay.removeEventListener(BlastChainCommand.EVENTTYPE_TANK_DIE, onGamePlay_tankDie);
		
		levelManager.removeEventListener(LevelManager.EVENTTYPE_ADD_NPCTANK, onLevelManager_addNPCTank);
		levelManager.removeEventListener(LevelManager.EVENTTYPE_NEW_CORPS, onLevelManager_newCorps);
		levelManager.removeEventListener(LevelManager.EVENTTYPE_MISSION_COMPLETE, onLevelManager_missionComplete);	
		
		mc_c.removeChild(gamePlay.getDO());
		gamePlay.dispose();
		gamePlay=null;
		levelManager.dispose();//要停掉timer
		levelManager=null;
	}
	
	//收到levelManger发事件可以添加坦克
	private function onLevelManager_addNPCTank(e:KEvent):void{
		gamePlay.executeCommand(AddNPCTankCommand.NAME, e.info);
		T.p("新NPC出现");
	}
	
	private function onLevelManager_newCorps(e:KEvent):void{
		var corpsVO:CorpsVO=e.info as CorpsVO;
		T.p("!!动画提示新军团出现",corpsVO.name);
		this.dispatchEvent(new KEvent(EVENTTYPE_NEW_CORPS,corpsVO));
	}
	
	private function onLevelManager_missionComplete(e:KEvent):void{
		T.p("战斗结束！");
		this.dispatchEvent(new KEvent(EVENTTYPE_MISSION_COMPLETE));
	}
	
	//坦克死亡
	private function onGamePlay_tankDie(e:KEvent):void{
		var vo:MyTankVO=e.info as MyTankVO;
		levelManager.onTankDie(vo);
		if(vo.teamID==GameEnum.TEAM_A){//自己坦克死亡
			T.p("游戏结束");
			this.dispatchEvent(new KEvent(EVENTTYPE_GAMEOVER));
		}
	}
	
	//准备好场景
	private function prepare():void{

		//添加entity
		var mapVO:MapVO=ResourceManagerX.getInstance().getMapVO();
		var groundVO:GroundVO=mapVO.groundVO;
		gamePlay.startup(groundVO.CC, groundVO.RC, groundVO.CellSize);//开始之前必须先执行这个
		
		//摄像机
		gamePlay.addEntity(new MyCameraEntity(groundVO.Width, groundVO.Height));
		
		//地面
		gamePlay.addEntity(new GroundEntity(groundVO));
		
		//障碍物
		for each(var blockVO:BlockVO in mapVO.dict_barVO){
			gamePlay.addEntity(new BlockEntity(blockVO));
		}
		
		//自己坦克.注意这个要放在start的后面，因为需要填充数据
		myTankVO=levelManager.createMyTankVO(1);
		myTankVO.name="鸟の诗";
		gamePlay.executeCommand(AddTankCommand.NAME, myTankVO);
	}

	private var myTankVO:MyTankVO;
	
	//开始游戏
	public function startPlay():LevelVO{
		//关卡管理器
		levelManager.start();
		
		//我的坦克进入激活状态
		(gamePlay.getWorld().findEntity(myTankVO.id) as LogicEntity).SwitchFSMState(GameEnum.FSMID_MAIN, GameEnum.STATE_GOOD);
	
		return levelManager.getLevelVO();
	}
	
	//时间到，冻结玩家
	public function deactivePlayer():void{
		if(gamePlay.getWorld().findEntity(myTankVO.id)){
			(gamePlay.getWorld().findEntity(myTankVO.id) as LogicEntity).SwitchFSMState(GameEnum.FSMID_MAIN, GameEnum.STATE_WAIT);
		}
		
	}
}//end of class
}
